home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / boot / decprom / RCS / fs.c,v < prev    next >
Encoding:
Text File  |  1991-08-31  |  8.9 KB  |  371 lines

  1. head     1.2;
  2. branch   ;
  3. access   ;
  4. symbols  ;
  5. locks    jhh:1.2; strict;
  6. comment  @ * @;
  7.  
  8.  
  9. 1.2
  10. date     90.06.27.14.57.09;  author shirriff;  state Exp;
  11. branches ;
  12. next     1.1;
  13.  
  14. 1.1
  15. date     90.02.16.16.14.10;  author shirriff;  state Exp;
  16. branches ;
  17. next     ;
  18.  
  19.  
  20. desc
  21. @@
  22.  
  23.  
  24. 1.2
  25. log
  26. @*** empty log message ***
  27. @
  28. text
  29. @/* fs.c -
  30.  *
  31.  *    General filesystem support.
  32.  *
  33.  * Copyright (C) 1985 Regents of the University of California
  34.  * All rights reserved.
  35.  */
  36.  
  37. #ifdef notdef
  38. static char rcsid[] = "$Header: /sprite/src/boot/decprom/RCS/fs.c,v 1.1 90/02/16 16:14:10 shirriff Exp Locker: shirriff $ SPRITE (Berkeley)";
  39. #endif not lint
  40.  
  41. #include "sprite.h"
  42. #include "fsBoot.h"
  43. #include "kernel/machMon.h"
  44.  
  45. /*
  46.  * For non-block aligned reads.
  47.  */
  48. char    readBuffer[FS_BLOCK_SIZE];
  49.  
  50. /*
  51.  * For lookup
  52.  */
  53. static char    component[FS_MAX_NAME_LENGTH];
  54.  
  55. /*
  56.  * Forward declarations.
  57.  */
  58. void FsGetFileDesc();
  59. void FsInitFileHandle();
  60.  
  61. /*
  62.  * ----------------------------------------------------------------------------
  63.  *
  64.  * Fs_Open --
  65.  *
  66.  *    Open a file.  This does a simple lookup (based on the kernel's
  67.  *    FsLocalLookup) and creates a handle for the file.
  68.  *
  69.  * Results:
  70.  *    SUCCESS or a return code from various sub-operations.
  71.  *
  72.  * Side effects:
  73.  *    Calls malloc
  74.  *
  75.  * ----------------------------------------------------------------------------
  76.  */
  77.  
  78. ReturnStatus
  79. Fs_Open(fileName, useFlags, permissions, handlePtrPtr)
  80.     char *fileName;
  81.     int useFlags;
  82.     int permissions;
  83.     Fsio_FileIOHandle     **handlePtrPtr;
  84. {
  85.     register ReturnStatus status;
  86.     Fsio_FileIOHandle *curHandlePtr;
  87.     register char *curCharPtr;
  88.     register char *componentPtr;
  89.     register int index;
  90.  
  91.     curCharPtr = fileName;
  92.     while(*curCharPtr == '/') {
  93.     curCharPtr++;
  94.     }
  95.     curHandlePtr = fsRootHandlePtr;
  96.  
  97.     while (*curCharPtr != '\0') {
  98.     if (curHandlePtr->descPtr->fileType != FS_DIRECTORY) {
  99.         return(FS_NOT_DIRECTORY);
  100.     }
  101.         /*
  102.          * Get the next component.
  103.          */
  104.         index = 0;
  105.     componentPtr = component;
  106.         while (*curCharPtr != '/' && *curCharPtr != '\0') {
  107.             *componentPtr++ = *curCharPtr++;
  108.         }
  109.         *componentPtr = '\0';
  110. #ifndef NO_PRINTF
  111.     Mach_MonPrintf(" %s ", component);
  112. #endif
  113.         /*
  114.          * Skip intermediate and trailing slashes so that *curCharPtr
  115.          * is Null when 'component' has the last component of the name.
  116.          */
  117.         while (*curCharPtr == '/') {
  118.             curCharPtr++;
  119.         }
  120.  
  121.     status = FsFindComponent(fsDomainPtr, curHandlePtr, component,
  122.                           &curHandlePtr);
  123.  
  124.     if (status != SUCCESS) {
  125. #ifndef NO_PRINTF
  126.         Mach_MonPrintf("<%x>\n", status);
  127. #endif
  128.         return(status);
  129.     }
  130.     }
  131.     *handlePtrPtr = curHandlePtr;
  132. }
  133.  
  134. /*
  135.  * ----------------------------------------------------------------------------
  136.  *
  137.  * Fs_Read --
  138.  *
  139.  *    Read from a file given its handle.
  140.  *
  141.  * Results:
  142.  *    A return status from the read.
  143.  *
  144.  * Side effects:
  145.  *    buffer is loaded with the data read in.
  146.  *    *readCountPtr is updated to reflect the number of bytes read.
  147.  *
  148.  * ----------------------------------------------------------------------------
  149.  */
  150. ReturnStatus
  151. Fs_Read(handlePtr, offset, numBytes, buffer, readCountPtr)
  152.     register Fsio_FileIOHandle     *handlePtr;
  153.     int            offset;
  154.     int            numBytes;
  155.     register Address    buffer;
  156.     int            *readCountPtr;
  157. {
  158.     int                firstBlock;
  159.     int                lastBlock;
  160.     int                lastByte;
  161.     BlockIndexInfo        indexInfo;
  162.     register    int        readSize;
  163.     register    int        blockAddr;
  164.     register    int        blockOffset;
  165.     register    int        bufferIndex;
  166.     register     ReturnStatus    status;
  167.     register    int        size;
  168.  
  169.     firstBlock = offset / FS_BLOCK_SIZE; 
  170.     lastByte = offset + numBytes - 1;
  171.     if (lastByte > handlePtr->descPtr->lastByte) {
  172.     lastByte = handlePtr->descPtr->lastByte;
  173.     }
  174.     lastBlock = lastByte / FS_BLOCK_SIZE;
  175.  
  176.     (void)FsGetFirstIndex(handlePtr, firstBlock, &indexInfo);
  177.  
  178.     bufferIndex = 0;
  179.     blockOffset = offset & FS_BLOCK_OFFSET_MASK;
  180. #ifdef SCSI0_BOOT 
  181.     Mach_MonPrintf(" read %d at %d into %x\n", numBytes, offset, buffer);
  182. #endif 
  183.  
  184.     while (indexInfo.blockNum <= lastBlock) {
  185.     if (indexInfo.blockNum < lastBlock) {
  186.         size = FS_BLOCK_SIZE - blockOffset;
  187.         readSize = FS_BLOCK_SIZE;
  188.     } else {
  189.         size = (lastByte & FS_BLOCK_OFFSET_MASK) + 1 - blockOffset;
  190.         readSize = size;
  191.     }
  192.     blockAddr = *indexInfo.blockAddrPtr + 
  193.             fsDomainPtr->headerPtr->dataOffset * FS_FRAGMENTS_PER_BLOCK;
  194.     if (blockOffset != 0 || size != FS_BLOCK_SIZE) { 
  195.         status = FsDeviceBlockIO(FS_READ, &fsDevice, blockAddr,
  196.                (readSize - 1) / FS_FRAGMENT_SIZE + 1, readBuffer);
  197.         if (status != SUCCESS) {
  198.         goto readError;
  199.         }
  200.         bcopy(&(readBuffer[blockOffset]), &(buffer[bufferIndex]), size);
  201.     } else {
  202.         status = FsDeviceBlockIO(FS_READ, &fsDevice, blockAddr,
  203.             FS_FRAGMENTS_PER_BLOCK, &(buffer[bufferIndex]));
  204.         if (status != SUCCESS) {
  205.         goto readError;
  206.         }
  207.     }
  208.     bufferIndex += size;
  209.     blockOffset = 0;
  210.     FsGetNextIndex(handlePtr, &indexInfo);
  211.     }
  212.  
  213. readError:
  214.  
  215.     *readCountPtr = bufferIndex;
  216.  
  217.     return(status);
  218. }
  219.  
  220. /*
  221.  *----------------------------------------------------------------------
  222.  *
  223.  * FsFindComponent --
  224.  *
  225.  *
  226.  * Results:
  227.  *    None.
  228.  *
  229.  * Side effects:
  230.  *
  231.  *----------------------------------------------------------------------
  232.  */
  233. ReturnStatus
  234. FsFindComponent(domainPtr, curHandlePtr, component, newHandlePtrPtr)
  235.     Fsdm_Domain *domainPtr;
  236.     Fsio_FileIOHandle *curHandlePtr;
  237.     char *component;
  238.     Fsio_FileIOHandle **newHandlePtrPtr;
  239. {
  240.     register ReturnStatus status;
  241.     register int dirOffset;        /* Offset within the directory */
  242.     register int blockOffset;        /* Offset within a directory block */
  243.     register Fslcl_DirEntry *dirEntryPtr;    /* Reference to directory entry */
  244.     int length;                /* Length variable for read call */
  245.     register Fsio_FileIOHandle *handlePtr;
  246.  
  247.     dirOffset = 0;
  248.     do {
  249.     length = FSLCL_DIR_BLOCK_SIZE;
  250.     status = Fs_Read(curHandlePtr, dirOffset, length, readBuffer, &length);
  251.     if (status != SUCCESS) {
  252.         return(status);
  253.     }
  254.     if (length == 0) {
  255.         return(FS_FILE_NOT_FOUND);
  256.     }
  257.     dirEntryPtr = (Fslcl_DirEntry *)readBuffer;
  258.     blockOffset = 0;
  259.     while (blockOffset < FSLCL_DIR_BLOCK_SIZE) {
  260.         dirEntryPtr = (Fslcl_DirEntry *)((int)readBuffer + blockOffset);
  261.         if (dirEntryPtr->fileNumber != 0) {
  262.         /*
  263.          * A valid directory record.
  264.          */
  265. #ifndef NO_PRINTF
  266.         Mach_MonPrintf("Found %s\n", dirEntryPtr->fileName);
  267. #endif NO_PRINTF
  268.         if (*dirEntryPtr->fileName=='\0') {
  269.         /*
  270.          * This check is a hack.  Something else should be done.
  271.          */
  272.             return (FS_FILE_NOT_FOUND);
  273.         }
  274.         if (strcmp(component, dirEntryPtr->fileName) == 0) {
  275.             handlePtr = (Fsio_FileIOHandle *)malloc(sizeof(Fsio_FileIOHandle));
  276.             FsInitFileHandle(domainPtr, dirEntryPtr->fileNumber,
  277.                     handlePtr);
  278.             *newHandlePtrPtr = handlePtr;
  279.             return(SUCCESS);
  280.         }
  281.         }
  282.         blockOffset += dirEntryPtr->recordLength;
  283.     }
  284.     dirOffset += FSLCL_DIR_BLOCK_SIZE;
  285.     } while(TRUE);
  286. }
  287.  
  288. /*
  289.  *----------------------------------------------------------------------
  290.  *
  291.  * FsInitFileHandle --
  292.  *
  293.  *    Initialize a file handle.
  294.  *
  295.  * Results:
  296.  *    None.
  297.  *
  298.  * Side effects:
  299.  *    Fills in the file handle that our caller has already allocated.
  300.  *
  301.  *----------------------------------------------------------------------
  302.  */
  303. void
  304. FsInitFileHandle(domainPtr, fileNumber, handlePtr)
  305.     Fsdm_Domain *domainPtr;
  306.     int fileNumber;
  307.     register Fsio_FileIOHandle *handlePtr;
  308. {
  309.     register Fsdm_FileDescriptor *descPtr;
  310.  
  311.     bzero((Address)handlePtr, sizeof(Fsio_FileIOHandle));
  312.     handlePtr->hdr.fileID.minor = fileNumber;
  313.     descPtr = (Fsdm_FileDescriptor *)malloc(sizeof(Fsdm_FileDescriptor));
  314.     FsGetFileDesc(domainPtr, fileNumber, descPtr);
  315.     handlePtr->descPtr = descPtr;
  316. }
  317.  
  318. /*
  319.  *----------------------------------------------------------------------
  320.  *
  321.  * FsGetFileDesc --
  322.  *
  323.  *    Read in a file descriptor from the disk.
  324.  *
  325.  * Results:
  326.  *    None.
  327.  *
  328.  * Side effects:
  329.  *    Fills in the file descriptor that our caller has already allocated.
  330.  *
  331.  *----------------------------------------------------------------------
  332.  */
  333. void
  334. FsGetFileDesc(domainPtr, fileNumber, descPtr)
  335.     Fsdm_Domain *domainPtr;
  336.     register int fileNumber;
  337.     register Fsdm_FileDescriptor *descPtr;
  338. {
  339.     register Fsdm_DomainHeader *headerPtr;
  340.     register int         blockNum;
  341.     register int         offset;
  342.  
  343.     headerPtr = domainPtr->headerPtr;
  344.     blockNum = headerPtr->fileDescOffset + fileNumber / FSDM_FILE_DESC_PER_BLOCK;
  345.     offset = (fileNumber & (FSDM_FILE_DESC_PER_BLOCK - 1)) *
  346.         FSDM_MAX_FILE_DESC_SIZE;
  347.  
  348.     (void)FsDeviceBlockIO(FS_READ, &fsDevice, 
  349.                blockNum * FS_FRAGMENTS_PER_BLOCK,
  350.                FS_FRAGMENTS_PER_BLOCK, readBuffer);
  351.     bcopy( readBuffer + offset, descPtr, sizeof(Fsdm_FileDescriptor));
  352. #ifndef NO_PRINTF
  353.     if (descPtr->magic != FSDM_DISK_MAGIC) {
  354.     Mach_MonPrintf("desc %d bad <%x>\n", fileNumber, descPtr->magic);
  355.     }
  356. #endif
  357. }
  358. @
  359.  
  360.  
  361. 1.1
  362. log
  363. @Initial revision
  364. @
  365. text
  366. @d10 1
  367. a10 1
  368. static char rcsid[] = "$Header: /sprite/src/boot/dsprom/RCS/fs.c,v 1.1 90/02/13 23:40:32 shirriff Exp $ SPRITE (Berkeley)";
  369. d240 6
  370. @
  371.